//
//  KVeritificationViewController.m
//  Keping
//
//  Created by 柯平 on 2017/6/30.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "KVeritificationViewController.h"
#import "KTitleTextCell.h"
#import "KSelectCommunityController.h"
#import "KGpsCommunityRequest.h"

@interface KVeritificationViewController ()
{
    NSArray* items;
}
/**/
@property(nonatomic,strong)UIView* footView;
/**/
@property(nonatomic,strong)UIButton* addButton;

@property(nonatomic, strong)KCommunityEntityView *selectEntity;

@end

@implementation KVeritificationViewController

- (KCommunityEntityView *)selectEntity {
    if (!_selectEntity) {
        _selectEntity = [[KCommunityEntityView alloc] init];
    }
    return _selectEntity;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"Veritification";
    
    items = @[@"*Community:",@"*Full Name:",@"Verification Code:",@"*Unit No:"];
    
    self.groupView.refreshEnable = NO;
    self.groupView.loadmoreEnable = NO;
    self.groupView.tableFooterView = self.footView;
    [self.view addSubview:self.groupView];
    
}

-(void)submitVertificationInfo{
    [self.view endEditing:YES];
    [self showLoading];
    KCommunityVeritifyRequest *request = [[KCommunityVeritifyRequest alloc] init];
    request.params = @{
                       @"communityId":self.selectEntity.id,//社区ID
                       @"realyName":self.selectEntity.fullName,//真实姓名
                       @"unitNo":self.selectEntity.address,//单元号
                       @"verificationCode":self.selectEntity.verificationCode//验证码
                       };
    WEAKSELF
    [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf hideHUD];
        NSDictionary *dict = request.responseObject;
        NSInteger status = [[dict objectForKey:@"statusCode"] integerValue];
        if (status == 200) {
            [weakSelf showSuccess:[dict objectForKey:@"msg"]];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } else {
            [weakSelf showErrorText:[dict objectForKey:@"msg"] errCode:status];
        }
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf hideHUD];
        [weakSelf showError:request.error];
    }];
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section
{
    return 5.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return items.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = indexPath.section;
    NSString* title = items[section];
    KTitleTextCell* cell = [tableView dequeueReusableCellWithIdentifier:[KTitleTextCell className]];
    if (!cell) {
        cell = [[KTitleTextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[KTitleTextCell className]];
    }
    if ([@"*Community:" isEqualToString:title]) {
        cell.textTf.userInteractionEnabled = NO;
    }
    cell.titleLb.text = title;
    switch (section) {
        case 0:
            cell.textTf.text = self.selectEntity.name;
            break;
        case 1:
            cell.textTf.text = self.selectEntity.fullName;
            break;
        case 2:
            cell.textTf.text = self.selectEntity.verificationCode;
            break;
        case 3:
            cell.textTf.text = self.selectEntity.address;
            break;
        default:
            break;
    }
    WEAKSELF
    cell.CommunityForText = ^(NSString *text) {
        if (section == 1) {
            weakSelf.selectEntity.fullName = text;
        } else {
            weakSelf.selectEntity.verificationCode = text;
        }
    };
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* title = items[indexPath.section];
    if ([@"*Community:" isEqualToString:title]) {
        WEAKSELF
        KSelectCommunityController* vc = [KSelectCommunityController new];
        vc.entity = ^(KCommunityEntityView *entity) {
            weakSelf.selectEntity.address = entity.address;
            weakSelf.selectEntity.createTime = entity.createTime;
            weakSelf.selectEntity.id = entity.id;
            weakSelf.selectEntity.latitude = entity.latitude;
            weakSelf.selectEntity.longitude = entity.longitude;
            weakSelf.selectEntity.name = entity.name;
            weakSelf.selectEntity.propertyId = entity.propertyId;
            [weakSelf.groupView reloadData];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

-(UIView *)footView
{
    if (!_footView) {
        _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 84)];
        _footView.backgroundColor = kClearColor;
        [self.addButton setFrame:CGRectMake(15, 30, kScreenWidth-30, 44)];
        [_footView addSubview:self.addButton];
    }
    return _footView;
}

-(UIButton *)addButton
{
    if (!_addButton) {
        _addButton = [UIButton commonButtonWithTitle:@"Submit"];
        [_addButton addTarget:self action:@selector(submitVertificationInfo) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
